package com.xinqi.modules.chat.chat.group.controller.inner;

import com.xinqi.common.base.response.R;
import com.xinqi.common.base.user.Account;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.chat.chat.group.dto.req.ChatClassUserGroupCreateDTO;
import com.xinqi.modules.chat.chat.group.dto.rsp.ChatGroupResultDTO;
import com.xinqi.modules.chat.chat.group.service.ChatGroupService;
import com.xinqi.modules.chat.chat.group.service.ChatGroupUserService;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Objects;


/**
 * 聊天模块
 *
 * @Author：yyzhen
 * @Package：com.xinqi.modules.chat.chat.chat.controller.inner
 * @Project：xingqiclass-cloud-plus
 * @name：ChatInnerController
 * @Date：2023/3/10 14:17
 * @Filename：ChatInnerController
 */
@Hidden
@Validated
@RestController
@RequestMapping("/inner/chat")
@RequiredArgsConstructor
public class ChatInnerController {

    private final ChatGroupService chatGroupService;

    private final ChatGroupUserService chatGroupUserService;

    /**
     * 账号注销（聊天板块变动）
     *
     * @Title:
     * @MethodName: accountCancel
     * []
     * @Return com.xinqi.common.base.response.R<java.lang.Boolean>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/10 15:34
     */
    @PostMapping("/accountCancel")
    public R<Boolean> accountCancel(@RequestParam(value = "userId") Long userId) {
        /*
         * 群主 -- 转让群组
         * 群成员 -- 退出群聊
         * 好友 -- 删除好友
         */
        return R.data(chatGroupService.accountCancel(userId));
    }

    /**
     * 创建班级群
     *
     * @param dto
     * @Title:
     * @MethodName: classCreateGroup
     * @Return com.xinqi.common.base.response.R<com.xinqi.modules.chat.chat.group.dto.rsp.ChatGroupResultDTO>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/30 9:22
     */
    @Operation(summary = "老师（群主）--创建班级群")
    @PostMapping("/class-create-group")
    public R<ChatGroupResultDTO> classCreateGroup(@Validated @RequestBody ChatClassUserGroupCreateDTO dto) {
        Account account = new Account();
        if (Objects.isNull(dto.getUserId())) {
            account = LoginHelper.getXqLoginUser();
        } else {
            account = LoginHelper.getXqLoginUser(dto.getUserId());
        }
        return R.data(chatGroupService.classCreate(account, dto));
    }

    /**
     * 解散班级群
     *
     * @param classId 班级ID
     * @Title:
     * @MethodName: classDisbandGroup
     * @Return com.xinqi.common.base.response.R<java.lang.Boolean>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/30 9:36
     */
    @Operation(summary = "老师（群主）-解散班级群")
    @PostMapping("/class-disband-group")
    public R<Boolean> classDisbandGroup(@NotNull(message = "{chat.group.classId.not.blank}") @RequestParam("classId") Long classId) {
        return R.data(chatGroupService.classDisbandGroup(classId));
    }

    /**
     * 转让班级群
     *
     * @param classId 班级ID
     * @param userId  转让接收人用户ID
     * @Title:
     * @MethodName: classShiftGroup
     * @Return com.xinqi.common.base.response.R<java.lang.Boolean>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/30 9:37
     */
    @Operation(summary = "老师（群主）-转让班级群")
    @PostMapping("/class-shift-group")
    public R<Boolean> classShiftGroup(@NotNull(message = "{chat.group.classId.not.blank}") @RequestParam("classId") Long classId, @NotNull(message = "{chat.group.shift.userId.not.blank}") @RequestParam("userId") Long userId) {
        return R.data(chatGroupService.classShiftGroup(classId, userId));
    }

    /**
     * 退出班级群
     *
     * @param classId 班级ID
     * @param userId  退出班级群用户ID
     * @Title:
     * @MethodName: classExitGroup
     * @Return com.xinqi.common.base.response.R<java.lang.Boolean>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/30 9:38
     */
    @Operation(summary = "退出班级群")
    @PostMapping("/class-exit-group")
    public R<Boolean> classExitGroup(@NotNull(message = "{chat.group.classId.not.blank}") @RequestParam("classId") Long classId, @NotNull(message = "{chat.group.exit.userId.not.blank}") @RequestParam("userId") Long userId) {
        return R.data(chatGroupUserService.classExitGroup(classId, userId));
    }

    /**
     * 加入班级群
     *
     * @param classId 班级ID
     * @param userId  加入班级群用户ID
     * @Title:
     * @MethodName: classJoinGroup
     * @Return com.xinqi.common.base.response.R<java.lang.Boolean>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/4/7 10:17
     */
    @Operation(summary = "加入班级群")
    @PostMapping("/class-join-group")
    public R<Boolean> classJoinGroup(@NotNull(message = "{chat.group.classId.not.blank}") @RequestParam("classId") Long classId, @NotNull(message = "{chat.group.join.userId.not.blank}") @RequestParam("userId") Long userId) {
        return R.data(chatGroupUserService.classJoinGroup(classId, userId));
    }

}
